﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class Controles_cboNumerico : System.Web.UI.UserControl
{
    private NotaTipoIngreso ntTipo = NotaTipoIngreso.Combo;
    private string _Value;
    private string _Text;
    private string _valueInit="";
    private int _forzarTipo = -1;
    private Boolean _NotasConceptuales = false;


    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<script language='javascript'>");
        sb.Append("function validar(otxt){");
        sb.Append("if(event.keyCode==97 || event.keyCode==65){otxt.value='A';return false;     };");
        sb.Append("if( otxt.value=='A'){otxt.value='';}");
        sb.Append("if ( (event.keyCode <48 || event.keyCode >=58 )  && (event.keyCode != 46)      ) return false; ");
        sb.Append("}</script>");
        Page.ClientScript.RegisterClientScriptBlock( Page.GetType(),"validarcboNUmeric",sb.ToString());
        
    

        if (cboNumerico.Items.Count==0)
        {
           // switch (1) 
            int Tipo;
            if (_forzarTipo >= 0)
            {
                Tipo = _forzarTipo;
            }
            else
            {
                Tipo = biFactory.Materia().idTipoPlanilla;
            }
            switch (Tipo) 
            { 
                case 1: //1° a 4° Grado: SON BIMESTRES Y SE CARGAN MENSUALMENT
                    TipoIngreso = NotaTipoIngreso.Letra;

                    string sSql;
                    if (biFactory.Materia().IdIdioma != 0 && !_NotasConceptuales)
                    {
                        sSql = "select * from tiponotas where Idioma=" + biFactory.Materia().IdIdioma;
                    }
                    else
                    {
                        sSql = "select * from tiponotas where Idioma=1" ;
                    }

                    if (_NotasConceptuales)
                    {
                        sSql += " and numero in (10,9,8,7,6.50,5,4,3,0) ";
                    }

                     sSql+= " order by numero desc";
                    DataTable dt = SchoolsBI.TipoNotas.Tabla(sSql);
                    DataRow dr;
                    dr = dt.NewRow();
                    dr["Numero"] = "-1";
                    dr["Detalle"] = "Seleccione...";
                    dt.Rows.InsertAt(dr, 0);

                    cboNumerico.Items.Clear();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow r = dt.Rows[i];
                        cboNumerico.Items.Add(new ListItem(r["Detalle"].ToString(), r["Numero"].ToString()));
                        ListItem l = cboNumerico.Items[i];
                        if (Single.Parse(r["Numero"].ToString()) >= 6) { l.Attributes.CssStyle.Add("color", "blue"); }
                        if (Single.Parse(r["Numero"].ToString()) >= 4 && Single.Parse(r["Numero"].ToString()) < 6) { l.Attributes.CssStyle.Add("color", "green"); }
                        if (Single.Parse(r["Numero"].ToString()) < 4 && Single.Parse(r["Numero"].ToString()) > 0) { l.Attributes.CssStyle.Add("color", "red"); }
                        if (Single.Parse(r["Numero"].ToString()) == 0) { l.Attributes.CssStyle.Add("color", "gray"); }
                    }

                    break;
                case 2:
                    TipoIngreso = NotaTipoIngreso.Numerico;
                    break;
                case 3 :
                    TipoIngreso = NotaTipoIngreso.Numerico;
                    break;
                case 4:
                    TipoIngreso = NotaTipoIngreso.Numerico;
                    break;
            }
        }
    }

    public int ForzarTipo
    {
        set { _forzarTipo =value; }
    }

    public Boolean NotaConceptual {

        set { _NotasConceptuales = value; }
        get { return _NotasConceptuales; }
    }

    public NotaTipoIngreso TipoIngreso
    {
        set { ntTipo = value;
        switch (ntTipo) {
            case NotaTipoIngreso.Letra:
                cboNumerico.Visible = true;
                break;
            case NotaTipoIngreso.Combo:
                cboNumerico.Visible = true;
                break;
            case NotaTipoIngreso.Numerico:
                txtNumero.Visible = true;
                break;
            default:
                break;
        }
            
        }
        get { return ntTipo; }
    }

        public string Selected{
            set
            {
                try
                {
                    if (txtValueInit.Text == "") 
                    {
                        if (value!="sn")
                            if (value == "0.00") { txtValueInit.Text = "A"; } else { txtValueInit.Text = value; }
                    }
                    if (value != "sn")
                        if (value == "0.00") { txtNumero.Text = "A"; } else { txtNumero.Text = value; }
                        
                    if (cboNumerico.Items.FindByValue(value.ToString()) != null)
                    {
                        cboNumerico.SelectedValue = value.ToString();
                    }
                }
                catch (Exception e) {
                    cboNumerico.SelectedIndex =0;
                }
            }
            get { 
                
                return cboNumerico.SelectedValue; 
            }
        }
    public Single Value
    {
        get {
            Single sValor=-1;
            switch ((int)ntTipo) {
                case 0:
                    if (cboNumerico.SelectedValue != "")
                    {
                        sValor = Convert.ToSingle(cboNumerico.SelectedValue);
                    }
                    else
                    {
                        sValor = 0;
                    }

                    break;
                case 1:
                case 3:
                case 4:
                    if (txtNumero.Text == "A") { sValor = 0; }
                    else
                    {
                        if (!Single.TryParse(txtNumero.Text, out sValor)) sValor = -1;
                    }
                    break;
            }
            return sValor;

            
        }
    }

    public bool Borrado
    {
        get { return (txtValueInit.Text.Length>0 && this.Value.ToString()=="-1"); }
    }
    public bool Enabled {
        set { 
            cboNumerico.Enabled = value;
            txtNumero.Enabled = value;
        }
        get { 
            return cboNumerico.Enabled; 
        }
    }

    protected void txtNumero_TextChanged(object sender, EventArgs e)
    {
        _Value = txtNumero.Text;
    }
}

public enum NotaTipoIngreso { Combo, Numerico, Letra, Ingles }